#include "Sampler.h"


Sampler::Sampler(Graphics& gfx, Type type, bool reflect, UINT slot)
	:
	type(type),
	reflect(reflect),
	slot(slot)
{

	D3D11_SAMPLER_DESC samplerDesc = CD3D11_SAMPLER_DESC{ CD3D11_DEFAULT{} };
	samplerDesc.Filter = [type]() {
		switch (type)
		{
		case Type::Anisotropic: return D3D11_FILTER_ANISOTROPIC;
		case Type::Point: return D3D11_FILTER_MIN_MAG_MIP_POINT;
		default:
		case Type::Bilinear: return D3D11_FILTER_MIN_MAG_MIP_LINEAR;
		}
	}();
	samplerDesc.AddressU = reflect ? D3D11_TEXTURE_ADDRESS_MIRROR : D3D11_TEXTURE_ADDRESS_WRAP;
	samplerDesc.AddressV = reflect ? D3D11_TEXTURE_ADDRESS_MIRROR : D3D11_TEXTURE_ADDRESS_WRAP;
	samplerDesc.MaxAnisotropy = D3D11_REQ_MAXANISOTROPY;

	HR(GetDevice(gfx)->CreateSamplerState(&samplerDesc, &pSampler));
}

void Sampler::Bind(Graphics& gfx)
{
	GetContext(gfx)->PSSetSamplers(slot, 1, pSampler.GetAddressOf());
}
std::shared_ptr<Sampler> Sampler::Create(Graphics& gfx, Type type, bool reflect, UINT slot)
{
	return std::make_shared<Sampler>(gfx, type, reflect, slot);
}

